package com.leyou.mall.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leyou.mall.data.entity.ActivityTime;
import com.leyou.mall.data.entity.Result;
import com.leyou.mall.data.entity.Seckill;
import com.leyou.mall.data.vo.SeckillVO;
import com.leyou.mall.service.ActivityTimeService;
import com.leyou.mall.service.ISeckillService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: SeckillController
 * @author: LYL
 * @date: 2022/6/22 11:20
 * @Version 1.0
 */
@RestController
@RequestMapping("/seckill")
@Slf4j
public class SeckillController {

    @Resource
    private ISeckillService seckillService;

    @Resource
    private ActivityTimeService activityTimeService;

    /**
     * @Description: 分页全查活动信息
     * @param: [seckillVO]
     * @return: com.leyou.mall.data.entity.Result
     * @author: LYL
     * @date:2022/6/27 19:39
     */
    @PostMapping("/seckillList")
    public Result list(SeckillVO seckillVO){
        log.info("req::::::"+seckillVO);
        Integer pageNum = seckillVO.getPageNum();
        Integer pageSize = seckillVO.getPageSize();

        return Result.success(seckillService.findPage(new Page<>(pageNum, pageSize), seckillVO));

    }

    /**
     * @Description: 查询全部活动时段信息
     * @param: [seckill]
     * @return: com.leyou.mall.data.entity.Result
     * @author: LYL
     * @date:2022/6/27 19:39
     */
    @PostMapping("/getSeckill")
    public Result getSeckill(SeckillVO seckill){
        log.info("req::::::"+seckill);
        List<ActivityTime> activityTime = activityTimeService.getActivityTimeList(seckill.getSeckillNumber());

        return Result.success(activityTime);
    }

    /**
     * @Description: 查询商品数量
     * @param: [seckill]
     * @return: com.leyou.mall.data.entity.Result
     * @author: LYL
     * @date:2022/6/27 19:40
     */
    @PostMapping("/getCount")
    public Result getCount(SeckillVO seckill){
        log.info("req::::::::"+seckill);
        Integer count = activityTimeService.getCount(seckill.getSeckillNumber());
        if (count != null){
            return Result.success(count);
        }else {
            return Result.success(0);
        }

    }

    /**
     * @Description: 添加修改活动信息 
     * @param: [seckill]
     * @return: com.leyou.mall.data.entity.Result
     * @author: LYL
     * @date:2022/6/27 19:40
     */
    @PostMapping("/saveSeckill")
    public Result saveSeckill(Seckill seckill){
        log.info("req::::::::"+seckill);
        String simpleUUID = IdUtil.simpleUUID();

        Date date = DateUtil.date();
        if (seckill.getId() == null){
            seckill.setSeckillNumber(simpleUUID);
            seckill.setSeckillStatus("0");
            seckill.setDeleteFlag(false);
            seckill.setCreateDate(date);
            seckill.setUpdateDate(date);
            return Result.success(seckillService.save(seckill));
        }else {
            seckill.setUpdateDate(date);
            return Result.success(seckillService.save(seckill));
        }


    }

    /**
     * @Description: 添加修改活动时段信息
     * @param: [activityTime]
     * @return: com.leyou.mall.data.entity.Result
     * @author: LYL
     * @date:2022/6/27 19:41
     */
    @PostMapping("/saveActivity")
    public Result saveActivity(ActivityTime activityTime){
        log.info("req::::::::"+activityTime);
        String simpleUUID = IdUtil.simpleUUID();
        Date date = DateUtil.date();
        if (activityTime.getId() == null){
            activityTime.setActNumber(simpleUUID);
            activityTime.setActStatus("0");
            activityTime.setDeleteFlag(false);
            activityTime.setCreateDate(date);
            activityTime.setUpdateDate(date);
            return Result.success(activityTimeService.save(activityTime));
        }else {
            activityTime.setUpdateDate(date);
            return Result.success(activityTimeService.save(activityTime));
        }


    }

    /**
     * @Description: 修改是否下架
     * @param: [seckill]
     * @return: com.leyou.mall.data.entity.Result
     * @author: LYL
     * @date:2022/6/27 19:41
     */
    @PostMapping("/updateSType")
    public Result updateSType(Seckill seckill){

        Date date = DateUtil.date();

        seckill.setUpdateDate(date);
        if (seckill.getSeckillType()){
            if(!seckill.getSeckillStatus().equals("1")){
                return Result.success(seckillService.updateById(seckill));
            }else {
                return Result.error(400, "活动正在进行，请在活动结束后重试");
            }
        }else {
            return Result.success(seckillService.updateById(seckill));
        }


    }


    /**
     * @Description: 修改是否启用
     * @param: [activityTime]
     * @return: com.leyou.mall.data.entity.Result
     * @author: LYL
     * @date:2022/6/27 19:42
     */
    @PostMapping("/updateVType")
    public Result updateVType(ActivityTime activityTime){
        log.info("------------------" +activityTime);

        Date date = DateUtil.date();
        activityTime.setUpdateDate(date);

        if (!activityTime.getActType()){
            if (!activityTime.getActStatus().equals("1")){
                return Result.success(activityTimeService.updateById(activityTime));

            }else {
                return Result.error(400, "活动正在进行，请在活动结束后重试");
            }
        }else {
            return Result.success(activityTimeService.updateById(activityTime));
        }

    }


    /**
     * @Description: 删除活动
     * @param: [seckill]
     * @return: com.leyou.mall.data.entity.Result
     * @author: LYL
     * @date:2022/6/27 19:42
     */
    @PostMapping("/delSeckill")
    public Result delSeckill(Seckill seckill){
        Date date = DateUtil.date();
        seckill.setUpdateDate(date);
        seckill.setDeleteFlag(true);

        if (!seckill.getSeckillStatus().equals("1")){
            boolean b = seckillService.updateById(seckill);
            if (b){
                List<ActivityTime> activityTimeList = activityTimeService.getActivityTimeList(seckill.getSeckillNumber());
                for (ActivityTime activityTime : activityTimeList) {
                    activityTime.setUpdateDate(date);
                    activityTime.setDeleteFlag(true);
                }
                return Result.success(activityTimeService.updateBatchById(activityTimeList));
            }else {
                return Result.error();
            }
        }else {
            return Result.error(400, "活动正在进行，请在活动结束后重试");
        }









    }
}
